<template>
  <el-dialog v-model="dialogTableVisible" :title="title" width="90%">
    <div class="contract_base_title">评估明细</div>
    <merge-table
      v-if="loading"
      ref="mergeTableRef"
      style="margin-bottom: 10px"
      :showIndex="false"
      :tableData="tableList"
      :checkbox="false"
      :mergeFields="mergeFields"
      :column="scoringColumnList"
      :actionBtnIsShow="false"
    >
    </merge-table>
  </el-dialog>
</template>
<script setup>
import { ref, nextTick } from 'vue'
import MergeTable from '@/components/MergeTable.vue'
import { useMock } from '../hooks/mock.js'
import { ERR_OK } from 'config/statusCode'

import { getVendorKpiDetailByIdApi } from 'api/personalCenter'

// scoringColumnList
const { scoringColumnList } = useMock()
const dialogTableVisible = ref(false)
const loading = ref(false)
const mergeTableRef = ref()
const tableList = ref([])
const mergeFields = ref(['kpiQuotaTypeName', 'quotaTypeWeight', 'quotaWeight'])
const props = defineProps({
  title: {
    type: String,
    default: '评分进展'
  }
})

//展示
const showDialog = (kpiTaskId, id) => {
  dialogTableVisible.value = true
  // console.log(kpiTaskId, id)
  handelScoringTableList({ kpiTaskId: kpiTaskId, vendorId: id })
  // 注意： 根据考核模版的权重类型列表是否展示 指标类别权重、指标权重
}
// 获取数据
const handelScoringTableList = async (params) => {
  const { code, data } = await getVendorKpiDetailByIdApi(params)
  if (data.details?.length) {
    tableList.value = data.details
    data.details.forEach((item) => {
      item.quotaTypeId = item.kpiQuotaTypeId
      item.quotaTypeName = item.kpiQuotaTypeName
      if (item.takeScoreType == 10) {
        if (item.quotaResult) {
          item.quotaResult = item.quotaResult + '%'
        }
      }
    })
  }
  if (code == ERR_OK) {
    let len = scoringColumnList.length
    if (data.weightType == 10) {
      for (var i = len; i >= 0; i--) {
        if (scoringColumnList[i]?.fieldCode == 'quotaTypeWeight') {
          scoringColumnList.splice(i, 1)
        }
        if (scoringColumnList[i]?.fieldCode == 'quotaWeight') {
          scoringColumnList.splice(i, 1)
        }
      }
      mergeFields.value = ['kpiQuotaTypeName']
    }
    if (data.weightType == 20) {
      for (var j = len; j >= 0; j--) {
        if (scoringColumnList[j]?.fieldCode == 'quotaTypeWeight') {
          scoringColumnList.splice(j, 1)
          mergeFields.value = ['kpiQuotaTypeName', 'quotaWeight']
        }
      }
    }
  }
  loading.value = true
  tableList.value = data.details
  // console.log(tableList)
  nextTick(() => {
    mergeTableRef.value.toMergeColumn()
  })
}

// 更新数据
const handelRenewalData = () => {}

defineExpose({ showDialog })
</script>
